﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IndoReputation.Models.BusinessLogic;
using IndoReputation.Models.BusinessObject;
using IndoReputation.Views.ViewModels;
using IndoReputation.Commons;
using IndoReputation.Models.Constants;

namespace IndoReputation.Controllers
{
    [Authorize]
    public class ChartController : BaseController
    {
        public ActionResult Index()
        {
            UsersExtend currentUser = UsersBOL.GetUserExtendById(MySession.UserID);
            ViewData["selectedDate"] = DateTime.Now;
            ChartNewsModel model = new ChartNewsModel();
            var parent = NewsCategoryBOL.GetAllNewsCategory();
            List<NewsCategory> listNC = new List<NewsCategory>();
            listNC.Add(new NewsCategory { ID = 0, Name = "All" });
            GetAllNewsCategory(null, null, ref listNC, parent);
            model.trendNewsModel = NewsBOL.GetTrendNews(DateTime.Now, 0);
            model.ListNewsCategory = listNC;
            model.ListDisplayBy = CommonFunctions.ToSelectList<IRConstants.DisplayBy>(new IRConstants.DisplayBy());
            // comment below line because use ajax to load data to chart
            //model.ListChartData = NewsBOL.GetChartData(new DateTime(DateTime.Now.Year, 1, 1), new DateTime(DateTime.Now.Year, 12, 31), 0);
            model.ListChartData = null;
            model.ListChartDataCountry = null;
            model.ListChartDataRegion = null;
            model.CurrentUser = currentUser;
            model.ListRegion = new SelectList(RegionBOL.GetAllRegion(), "ID", "Name");
            model.ListCountry = new SelectList(CountryBOL.GetAllCountry(), "ID", "Name");
            model.TrendNewsModelCountry = NewsBOL.GetTrendNewsCountry(DateTime.Now, 0, currentUser.CountryID);
            model.TrendNewsModelRegion = NewsBOL.GetTrendNewsRegion(DateTime.Now, 0, currentUser.RegionID);
            return View(model);
        }

        public ActionResult _BuildChart(string dtFrom, string dtTo, int? newsCategory, int? displayBy)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;
            if (displayBy == 0)
            {
                return Json(NewsBOL.GetChartData(dateFrom, dateTo, (int)newsCategory));
            }
            else if (displayBy == 1)
            {
                return Json(NewsBOL.GetChartData(dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartCountry(string dtFrom, string dtTo, int? newsCategory, int? displayBy, int? countryID)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;
            countryID = countryID ?? 0;
            if (displayBy == 0)
            {
                return Json(NewsBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, (int)newsCategory));
            }
            else if (displayBy == 1)
            {
                return Json(NewsBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartRegion(string dtFrom, string dtTo, int? newsCategory, int? displayBy, int? regionID)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            displayBy = displayBy ?? 0;
            regionID = regionID ?? 0;
            if (displayBy == 0)
            {
                return Json(NewsBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, (int)newsCategory));
            }
            else if (displayBy == 1)
            {
                return Json(NewsBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, (int)newsCategory, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public void GetAllNewsCategory(int? parentId, NewsCategory parentNode, ref List<NewsCategory> afterBind, List<NewsCategory> source)
        {
            var filteredItems = source.Where(item => item.ParentID == parentId);
            NewsCategory n;
            foreach (var i in filteredItems)
            {
                NewsCategory _temp = new NewsCategory
                {
                    ChildLocations = i.ChildLocations,
                    CreateAt = i.CreateAt,
                    CreateBy = i.CreateBy,
                    Description = i.Description,
                    ID = i.ID,
                    Name = i.Name,
                    ParentID = i.ParentID,
                    UpdateAt = i.UpdateAt,
                    UpdateBy = i.UpdateBy
                };
                if (parentNode == null)
                {
                    afterBind.Add(_temp);
                }
                else
                {
                    parentNode.ChildLocations.Add(_temp);
                }
                n = _temp;
                GetAllNewsCategory(i.ID, n, ref afterBind, source);
            }
        }

        [HttpPost]
        public ActionResult _BuildTrend(string dtTrend, int? newsCategory)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            return Json(NewsBOL.GetTrendNews(dateTrend, (int)newsCategory));
        }

        [HttpPost]
        public ActionResult _BuildTrendCountry(string dtTrend, int? newsCategory, int? countryID)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            countryID = countryID ?? 0;
            return Json(NewsBOL.GetTrendNewsCountry(dateTrend, (int)newsCategory, (int)countryID));
        }

        [HttpPost]
        public ActionResult _BuildTrendRegion(string dtTrend, int? newsCategory, int? regionID)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            newsCategory = newsCategory ?? 0;
            regionID = regionID ?? 0;
            return Json(NewsBOL.GetTrendNewsRegion(dateTrend, (int)newsCategory, (int)regionID));
        }

        public ActionResult _BuildChartMedia(string dtFrom, string dtTo, int? displayBy)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }            
            displayBy = displayBy ?? 0;
            if (displayBy == 0)
            {
                return Json(MediaBOL.GetChartData(dateFrom, dateTo));
            }
            else if (displayBy == 1)
            {
                return Json(MediaBOL.GetChartData(dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartCountryMedia(string dtFrom, string dtTo, int? displayBy, int? countryID)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            displayBy = displayBy ?? 0;
            countryID = countryID ?? 0;
            if (displayBy == 0)
            {
                return Json(MediaBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo));
            }
            else if (displayBy == 1)
            {
                return Json(MediaBOL.GetChartDataCountry((int)countryID, dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        public ActionResult _BuildChartRegionMedia(string dtFrom, string dtTo, int? displayBy, int? regionID)
        {
            DateTime dateFrom = DateTime.Now;
            DateTime dateTo = DateTime.Now;
            if (string.IsNullOrEmpty(dtFrom))
            {
                dateFrom = new DateTime(DateTime.Now.Year, 1, 1);
            }
            else
            {
                string[] dt = dtFrom.Split('-');
                dateFrom = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            if (string.IsNullOrEmpty(dtTo))
            {
                dateTo = new DateTime(DateTime.Now.Year, 12, 31);
            }
            else
            {
                string[] dt = dtTo.Split('-');
                dateTo = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            displayBy = displayBy ?? 0;
            regionID = regionID ?? 0;
            if (displayBy == 0)
            {
                return Json(MediaBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo));
            }
            else if (displayBy == 1)
            {
                return Json(MediaBOL.GetChartDataRegion((int)regionID, dateFrom, dateTo, IRConstants.DisplayBy.Year));
            }
            return Json(new { success = false });
        }

        [HttpPost]
        public ActionResult _BuildTrendMedia(string dtTrend)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            //newsCategory = newsCategory ?? 0;
            return Json(MediaBOL.GetTrendNews(dateTrend));
        }

        [HttpPost]
        public ActionResult _BuildTrendCountry(string dtTrend, int? countryID)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            //newsCategory = newsCategory ?? 0;
            countryID = countryID ?? 0;
            return Json(MediaBOL.GetTrendNewsCountry(dateTrend, (int)countryID));
        }

        [HttpPost]
        public ActionResult _BuildTrendRegion(string dtTrend, int? regionID)
        {
            DateTime dateTrend = DateTime.Now;
            if (!string.IsNullOrEmpty(dtTrend))
            {
                string[] dt = dtTrend.Split('-');
                dateTrend = new DateTime(Convert.ToInt32(dt[0]), Convert.ToInt32(dt[1]), Convert.ToInt32(dt[2]));
            }
            //newsCategory = newsCategory ?? 0;
            regionID = regionID ?? 0;
            return Json(MediaBOL.GetTrendNewsRegion(dateTrend, (int)regionID));
        }

        public ActionResult Media()
        {
            ChartMediaModel model = new ChartMediaModel();
            UsersExtend currentUser = UsersBOL.GetUserExtendById(MySession.UserID);
            ViewData["selectedDate"] = DateTime.Now;
            
            model.trendNewsModel = MediaBOL.GetTrendNews(DateTime.Now);
            //model.ListNewsCategory = listNC;
            model.ListDisplayBy = CommonFunctions.ToSelectList<IRConstants.DisplayBy>(new IRConstants.DisplayBy());
            // comment below line because use ajax to load data to chart
            //model.ListChartData = NewsBOL.GetChartData(new DateTime(DateTime.Now.Year, 1, 1), new DateTime(DateTime.Now.Year, 12, 31), 0);
            model.ListChartData = null;
            model.ListChartDataCountry = null;
            model.ListChartDataRegion = null;
            model.CurrentUser = currentUser;
            model.ListRegion = new SelectList(RegionBOL.GetAllRegion(), "ID", "Name");
            model.ListCountry = new SelectList(CountryBOL.GetAllCountry(), "ID", "Name");
            model.TrendNewsModelCountry = MediaBOL.GetTrendNewsCountry(DateTime.Now, currentUser.CountryID);
            model.TrendNewsModelRegion = MediaBOL.GetTrendNewsRegion(DateTime.Now, currentUser.RegionID);
            return View(model);
        }        
    }
}
